import Vue from 'vue';
import App from './App.vue';
import router from './router';
import ElementUI from 'element-ui';
import VueI18n from 'vue-i18n';
import Websocket from './utils/websocket'
import { messages } from './components/common/i18n';
import 'element-ui/lib/theme-chalk/index.css'; // 默认主题
// import './assets/css/theme-green/index.css'; // 浅绿色主题
import './assets/css/icon.css';
import './components/common/directives';
import 'babel-polyfill';

import store from '@/vuex/store'

Vue.use(VueI18n);
Vue.use(ElementUI, {
    size: 'small'
});
const i18n = new VueI18n({
    locale: 'zh',
    messages
});

Vue.prototype.$response = {
    status:'',
    code:0,
    msg:'',
    data:''
}
Vue.config.productionTip = false;
Vue.prototype.$websocket = Websocket;
Websocket.dispatch('WEBSOCKET_INIT',"ws://192.168.0.3:19001")

Vue.prototype.sleep = function (numberMillis) {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime)
            return;
    }
}

Vue.prototype.Requests = function (id, body) {
    //readystate取值 描述

    //0 (WebSocket.CONNECTING)正在链接中
    //1 (WebSocket.OPEN) 已经链接并且可以通讯
    //2 (WebSocket.CLOSING)连接正在关闭
    //3 (WebSocket.CLOSED)连接已关闭或者没有链接成功

        console.log(this.$websocket.state.websock)
        if (this.$websocket.state.websock.readyState === 0) {
            i = i + 1
            console.log("WebSocket.CONNECTING:" + i)
            if ( i > 10) {
                this.$message.error("RECONNECTING FAILED! CHECK WEBSERVICE STATUS!");
            }
        } else if (this.$websocket.state.websock.readyState === 1) {
            console.log("WebSocket.OPEN")
            this.$websocket.dispatch("WEBSOCKET_SEND", {
                id: id,
                body: body,
            })
        } else if (this.$websocket.state.websock.readyState === 2) {
            console.log("WebSocket.CLOSING")
        } else if (this.$websocket.state.websock.readyState === 3) {
            console.log("WebSocket.CLOSED! RECONNECTING")
            this.$message.error("WebSocket.CLOSED! RECONNECTING!");
            this.$websocket.dispatch('WEBSOCKET_INIT', "ws://192.168.0.3:19001")
        }
}

//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | 亿维数据`;
    const role = localStorage.getItem('userinfo');
    if (!role && to.path !== '/login') {
        next('/login');
    } else if (to.meta.permission) {
        // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
        role === 'admin' ? next() : next('/403');
    } else {
        // 简单的判断IE10及以下不进入富文本编辑器，该组件不兼容
        if (navigator.userAgent.indexOf('MSIE') > -1 && to.path === '/editor') {
            Vue.prototype.$alert('vue-quill-editor组件不兼容IE10及以下浏览器，请使用更高版本的浏览器查看', '浏览器不兼容通知', {
                confirmButtonText: '确定'
            });
        } else {
            next();
        }
    }
});

new Vue({
    beforeCreate(){
        // 添加背景色f4f4f5
        document.querySelector('body').setAttribute('style', 'background-color:#f0f0f0')
    },
    beforeDestroy(){
        document.querySelector('body').setAttribute('style', '')
    },
    router,
    store,
    i18n,
    render: h => h(App)
}).$mount('#app');
